home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / rsxwin2a.zip / SHELL.C < prev    next >
C/C++ Source or Header  |  1994-02-19  |  5KB  |  248 lines

  1. #include <sys/emx.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <io.h>
  5. #include <string.h>
  6. #include <malloc.h>
  7. #include <process.h>
  8.  
  9. typedef __const__ char * __const__ * CONSTCHAR;
  10. static char **org_env;
  11.  
  12. static char *timestr( unsigned t, char *buf )
  13. {
  14.     sprintf( buf, "%2.2d:%02.2d", (t >> 11) & 0x1f, (t >> 5) & 0x3f);
  15.     return buf;
  16. }
  17.  
  18. static char *datestr( unsigned d, char *buf )
  19. {
  20.     sprintf( buf, "%2.2d-%2.2d-%2.2d", d&0x1f, (d>>5) & 0x0f, (d>>9) + 80 );
  21.     return buf;
  22. }
  23.  
  24. static long show_filename(struct _find *pfind)
  25. {
  26.     char timebuf[10], datebuf[10];
  27.     unsigned size = ((unsigned) pfind->size_hi << 16) + pfind->size_lo;
  28.  
  29.     datestr( pfind->date, datebuf );
  30.     timestr( pfind->time, timebuf );
  31.  
  32.     if (pfind->attr & (_A_SUBDIR | _A_VOLID))
  33.     printf( "%-15s  <DIR>\n", pfind->name);
  34.     else
  35.     printf( "%-15s %8u %8s %8s\n",
  36.         pfind->name, size, datebuf, timebuf);
  37.     return size;
  38. }
  39.  
  40. typedef enum {
  41.     DEFAULT, SET, ORGENV, DIR, CD, DRIVE, ECHO, DEL, HELP, EXIT
  42. } commands;
  43.  
  44. typedef struct {
  45.     char *cp;
  46.     commands t;
  47. } item;
  48.  
  49. static item cmds[] =
  50. {
  51.     "set", SET,
  52.     "orgenv", ORGENV,
  53.     "dir", DIR,
  54.     "cd", CD,
  55.     "drive", DRIVE,
  56.     "echo", ECHO,
  57.     "del", DEL,
  58.     "help", HELP,
  59.     "exit", EXIT,
  60.     NULL, 0
  61. };
  62.  
  63. static int execute_command(char *argv[])
  64. {
  65.     int i;
  66.     static char wild[]="*.*";
  67.     int cmd = 0;
  68.  
  69.     for (i = 0; cmds[i].cp; i++)
  70.     if (stricmp(cmds[i].cp, argv[0]) == 0) {
  71.         cmd = cmds[i].t;
  72.         break;
  73.     }
  74.     if (!cmd)
  75.     cmd = DEFAULT;
  76.  
  77.     switch (cmd) {
  78.     case SET:
  79.     if (argv[1]) {
  80.         char *e = malloc(strlen(argv[1]) + 2);
  81.         if (!e) return 0;
  82.         strcpy(e, argv[1]);
  83.         strupr(e);
  84.         if (putenv(e)) {
  85.         puts("Bad environmemt");
  86.         free(e);
  87.         }
  88.     }
  89.     else
  90.         for (i=0; environ[i] != NULL; i++)
  91.         puts(environ[i]);
  92.     break;
  93.  
  94.     case ORGENV:
  95.     environ = org_env;
  96.     break;
  97.  
  98.     case DIR:
  99.     {
  100.     struct _find info;
  101.     if (argv[1]==0)
  102.         argv[1]=wild;
  103.     if (__findfirst(argv[1],_A_NORMAL | _A_SUBDIR | _A_RDONLY, &info)) {
  104.         puts("no files found");
  105.         return 0;
  106.     }
  107.     show_filename(&info);
  108.     while (__findnext(&info) == 0)
  109.         show_filename(&info);
  110.     }
  111.     break;
  112.  
  113.     case CD:
  114.     if (argv[1])
  115.         _chdir2(argv[1]);
  116.     break;
  117.  
  118.     case DRIVE:
  119.     if (argv[1])
  120.         _chdrive(argv[1][0]);
  121.     break;
  122.  
  123.     case ECHO:
  124.     break;
  125.  
  126.     case DEL:
  127.     if (! argv[1])
  128.         return 0;
  129.     else if (strpbrk(argv[1],"*?")) {
  130.         struct _find info;
  131.         if (__findfirst(argv[1], _A_NORMAL, &info)) {
  132.         puts("no files found");
  133.         return 0;
  134.         }
  135.         else remove(info.name);
  136.         while (__findnext(&info) == 0)
  137.         remove(info.name);
  138.     }
  139.     else remove(argv[1]);
  140.     break;
  141.  
  142.     case HELP:
  143.     puts("commands: set, orgenv, cd, drive, dir, del, exit, help");
  144.     puts("          and EMX prgs and .bat prgs");
  145.     break;
  146.  
  147.     case EXIT:
  148.     exit(1);
  149.  
  150.     case DEFAULT:
  151.     if (argv[0][1] == ':') {
  152.         _chdrive(argv[0][0]);
  153.         break;
  154.     }
  155.     if (!strstr(argv[0], ".bat"))
  156.         return spawnvpe(P_WAIT, argv[0],
  157.                 (CONSTCHAR) argv, (CONSTCHAR) environ);
  158.  
  159.     } /* switch */
  160.  
  161.     return 0;
  162. }
  163.  
  164. int readline(FILE *fhandle, char *linebuf)
  165. {
  166.     int ch;
  167.     int i = 0;
  168.  
  169.     for (;;) {
  170.     ch = fgetc(fhandle);
  171.     if (ch == EOF) {
  172.         if (i == 0)
  173.         return -1;
  174.         linebuf[i] = 0;
  175.         return i;
  176.     }
  177.     else if (ch == '\n' || ch == '\r') {
  178.         linebuf[i] = 0;
  179.         return i;
  180.     }
  181.     else if (ch=='\t' || ch=='@') {
  182.         linebuf[i++]=' ';
  183.     }
  184.     else
  185.         linebuf[i++] = ch;
  186.     }
  187. }
  188.  
  189. /* build argv from string */
  190. int make_tokens(char *string, char *argv[])
  191. {
  192.     int argc,i = 0;
  193.     argv[0] = NULL;
  194.  
  195.     while (string[i]==' ')
  196.     i++;
  197.     if (string[i] == 0 || string[i] == '\n')
  198.     return 0;
  199.  
  200.     argc = 1;
  201.     argv[0] = string + i;
  202.     for (i = 0; string[i] != 0; i++)
  203.     if (string[i] == ' ') {
  204.         string[i] = 0;
  205.         if (string[i+1] == ' ')
  206.         continue;
  207.         if (string[i+1] == 0)
  208.         break;
  209.         argv[argc++] = string + i + 1;
  210.     }
  211.     argv[argc] = 0;
  212.     return argc;
  213. }
  214.  
  215. int main(int argc, char **argv)
  216. {
  217.     char cline[512];
  218.     char *argvec[64];
  219.  
  220.     org_env = environ;
  221.  
  222.     for (;;) {
  223.     cline[0] = _getdrive();
  224.     cline[1] = ':';
  225.     getcwd(cline+2, 256);
  226.     strlwr(cline);
  227.     printf("%s> ", cline);
  228.     if (readline(stdin, cline) < 0)
  229.         continue;
  230.     if (!make_tokens(cline, argvec))
  231.         continue;
  232.     if (strstr(argvec[0], ".bat")) {
  233.         FILE *fd = fopen(cline, "rt");
  234.         if (!fd)
  235.         continue;
  236.         while (readline(fd, cline) >= 0) {
  237.         if (!make_tokens(cline, argvec))
  238.             continue;
  239.         execute_command(argvec);
  240.         }
  241.         fclose(fd);
  242.     }
  243.     else
  244.         execute_command(argvec);
  245.     }
  246.     return 0;
  247. }
  248.